local autocmd = vim.api.nvim_create_autocmd

-- dont list quickfix buffers
autocmd("FileType", {
  pattern = "qf",
  callback = function()
    vim.opt_local.buflisted = false
  end,
})

local isOpenConfig = false;
vim.keymap.set("n", "<leader>cf", function()
  isOpenConfig = not isOpenConfig
  if isOpenConfig then
    vim.cmd('cd ' .. vim.fn.stdpath('config'))
    vim.cmd('edit init.vim')
  else
    vim.cmd('bufdo bd')
    vim.cmd('cd -')
  end
  vim.cmd('NvimTreeOpen')
end, { desc = 'configure nvim' })

local isOpenSouce = false;
vim.keymap.set("n", "<leader>cp", function()
  isOpenSouce = not isOpenSouce
  if isOpenSouce then
    vim.cmd('cd ' .. require('next.config').plugins.install_path)
    vim.cmd('enew')
  else
    vim.cmd('bufdo bd')
    vim.cmd('cd -')
  end
  vim.cmd('NvimTreeOpen')
end, { desc = 'see plugin source code' })

local function augroup(name)
  return vim.api.nvim_create_augroup("lazyvim_" .. name, { clear = true })
end
-- resize splits if window got resized
vim.api.nvim_create_autocmd({ "VimResized" }, {
  group = augroup("resize_splits"),
  callback = function()
    local current_tab = vim.fn.tabpagenr()
    vim.cmd("tabdo wincmd =")
    vim.cmd("tabnext " .. current_tab)
  end,
})

local is_transparent = true
local function config_theme(is_transparent)
  require("tokyonight").setup {
    -- your configuration comes here
    -- or leave it empty to use the default settings
    style = "moon",         -- The theme comes in three styles, `storm`, `moon`, a darker variant `night` and `day`
    light_style = "day",    -- The theme is used when the background is set to light
    transparent = is_transparent,     -- Enable this to disable setting the background color
    terminal_colors = true, -- Configure the colors used when opening a `:terminal` in [Neovim](https://github.com/neovim/neovim)
    styles = {
      -- Style to be applied to different syntax groups
      -- Value is any valid attr-list value for `:help nvim_set_hl`
      comments = { italic = true },
      keywords = { italic = true },
      functions = {},
      variables = {},
      -- Background styles. Can be "dark", "transparent" or "normal"
      sidebars = is_transparent and "transparent" or 'dark', -- style for sidebars, see below
      floats = is_transparent and "transparent" or 'dark',   -- style for floating windows
    },
    sidebars = { "qf", "help" },                             -- Set a darker background on sidebar-like windows. For example: `["qf", "vista_kind", "terminal", "packer"]`
    day_brightness = 0.3,                                    -- Adjusts the brightness of the colors of the **Day** style. Number between 0 and 1, from dull to vibrant colors
    hide_inactive_statusline = false,                        -- Enabling this option, will hide inactive statuslines and replace them with a thin border instead. Should work with the standard **StatusLine** and **LuaLine**.
    dim_inactive = false,                                    -- dims inactive windows
    lualine_bold = false,                                    -- When `true`, section headers in the lualine theme will be bold

    --- You can override specific color groups to use other groups or a hex color
    --- function will be called with a ColorScheme table
    ---@param colors ColorScheme
    on_colors = function(colors) end,

    --- You can override specific highlights to use other groups or a hex color
    --- function will be called with a Highlights and ColorScheme table
    ---@param highlights Highlights
    ---@param colors ColorScheme
    on_highlights = function(highlights, colors) end,
  }
end

return {
  {
    "folke/tokyonight.nvim",
    lazy = false,
    priority = 1000,
    opts = {},
    config = function()
      config_theme(is_transparent)
      vim.cmd [[colorscheme tokyonight]]
      vim.keymap.set('n', '<leader>ct', function()
        is_transparent = not is_transparent;
        config_theme(is_transparent)
        vim.cmd [[colorscheme tokyonight]]
      end, { desc = 'toggle transparent' })
    end,
  },

  {
    'akinsho/bufferline.nvim',
    branch = 'main',
    config = function()
      require('bufferline').setup()
    end,
    dependencies = 'nvim-tree/nvim-web-devicons'
  },
  {
    "nvim-lualine/lualine.nvim",
    opts = {},
  },


  {
    "goolord/alpha-nvim",
    dependencies = { "nvim-tree/nvim-web-devicons" },
    config = function()
      require("alpha").setup(require("alpha.themes.startify").config)
    end,
  },
  {
    "lukas-reineke/indent-blankline.nvim",
    main = "ibl",
    config = function()
      local highlight = {
        "RainbowRed",
        "RainbowYellow",
        "RainbowBlue",
        "RainbowOrange",
        "RainbowGreen",
        "RainbowViolet",
        "RainbowCyan",
      }

      local hooks = require "ibl.hooks"
      -- create the highlight groups in the highlight setup hook, so they are reset
      -- every time the colorscheme changes
      hooks.register(hooks.type.HIGHLIGHT_SETUP, function()
        vim.api.nvim_set_hl(0, "RainbowRed", { fg = "#E06C75" })
        vim.api.nvim_set_hl(0, "RainbowYellow", { fg = "#E5C07B" })
        vim.api.nvim_set_hl(0, "RainbowBlue", { fg = "#61AFEF" })
        vim.api.nvim_set_hl(0, "RainbowOrange", { fg = "#D19A66" })
        vim.api.nvim_set_hl(0, "RainbowGreen", { fg = "#98C379" })
        vim.api.nvim_set_hl(0, "RainbowViolet", { fg = "#C678DD" })
        vim.api.nvim_set_hl(0, "RainbowCyan", { fg = "#56B6C2" })
      end)

      require("ibl").setup { indent = { highlight = highlight } }
    end
  },

  {
    "NvChad/nvim-colorizer.lua",
    opts = {},
  },

  {
    "rcarriga/nvim-notify",
    opts = {
      stages = "static",
      timeout = 3000,
      render = "wrapped-compact",
    },
  },
  {
    "folke/noice.nvim",
    event = "VeryLazy",
    opts = {
      health = {
        checker = false,
      },
      -- routes = {
      --   filter = { event = "msg_show", kind = "search_count" },
      --   opts = { skip = true },
      -- },
      redirect = {
        -- view = "popup",
        filter = { event = "msg_show" },
      },
      lsp = {
        -- override markdown rendering so that **cmp** and other plugins use **Treesitter**
        override = {
          ["vim.lsp.util.convert_input_to_markdown_lines"] = true,
          ["vim.lsp.util.stylize_markdown"] = true,
          ["cmp.entry.get_documentation"] = true, -- requires hrsh7th/nvim-cmp
        },
      },
      -- you can enable a preset for easier configuration
      presets = {
        bottom_search = true,         -- use a classic bottom cmdline for search
        command_palette = true,       -- position the cmdline and popupmenu together
        long_message_to_split = true, -- long messages will be sent to a split
        inc_rename = true,            -- enables an input dialog for inc-rename.nvim
        lsp_doc_border = false,       -- add a border to hover docs and signature help
      },
    },                                -- add any options here
    dependencies = {
      -- if you lazy-load any plugin below, make sure to add proper `module="..."` entries
      "MunifTanjim/nui.nvim",
      -- OPTIONAL:
      --   `nvim-notify` is only needed, if you want to use the notification view.
      --
      --   If not available, we use `mini` as the fallback
      "rcarriga/nvim-notify",
    },
  }
}
